﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using MiniFox.Data.EntityFramework;
using MiniFox.Data.EntityFramework.MySql;

namespace Repository.Data.Entities.MySql;

public partial class RepositoryContext : MySqlDbContext
{
    public RepositoryContext(DbContextOptions<RepositoryContext> options)
        : base("RepositoryContext", options)
    {
    }

    public virtual DbSet<PublishLog> PublishLog { get; set; }

    public virtual DbSet<RepositorySet> RepositorySet { get; set; }

    public virtual DbSet<VersionItem> VersionItem { get; set; }

    public virtual DbSet<VersionSet> VersionSet { get; set; }

    public virtual DbSet<WorkItem> WorkItem { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .UseCollation("utf8mb4_0900_ai_ci")
            .HasCharSet("utf8mb4");

        modelBuilder.Entity<PublishLog>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PRIMARY");

            entity.ToTable("publish_log");

            entity.HasIndex(e => e.VersionId, "publish_log_version_item_FK");

            entity.Property(e => e.Id)
                .HasMaxLength(50)
                .HasColumnName("id");
            entity.Property(e => e.PublishTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("publish_time");
            entity.Property(e => e.PublisherId)
                .HasMaxLength(64)
                .HasColumnName("publisher_id");
            entity.Property(e => e.PublisherName)
                .HasMaxLength(100)
                .HasColumnName("publisher_name");
            entity.Property(e => e.VersionId).HasColumnName("version_id");
        });

        modelBuilder.Entity<RepositorySet>(entity =>
        {
            entity.HasKey(e => e.Name).HasName("PRIMARY");

            entity.ToTable("repository_set");

            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasColumnName("name");
            entity.Property(e => e.Description)
                .HasMaxLength(1000)
                .HasColumnName("description");
            entity.Property(e => e.UpdateTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("update_time");
        });

        modelBuilder.Entity<VersionItem>(entity =>
        {
            entity.HasKey(e => e.Id).HasName("PRIMARY");

            entity.ToTable("version_item");

            entity.HasIndex(e => new { e.VersionSetName, e.RepoName }, "version_item_version_set_FK");

            entity.Property(e => e.Id).HasColumnName("id");
            entity.Property(e => e.AuthorId)
                .HasMaxLength(64)
                .HasColumnName("author_id");
            entity.Property(e => e.AuthorName)
                .HasMaxLength(50)
                .HasColumnName("author_name");
            entity.Property(e => e.Comment)
                .HasMaxLength(1024)
                .HasColumnName("comment");
            entity.Property(e => e.Content)
                .HasColumnType("json")
                .HasColumnName("content");
            entity.Property(e => e.Operation)
                .HasMaxLength(20)
                .HasColumnName("operation");
            entity.Property(e => e.RepoName)
                .HasMaxLength(50)
                .HasColumnName("repo_name");
            entity.Property(e => e.VersionSetName)
                .HasMaxLength(50)
                .HasColumnName("version_set_name");
            entity.Property(e => e.VersionTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("version_time");
        });

        modelBuilder.Entity<VersionSet>(entity =>
        {
            entity.HasKey(e => new { e.Name, e.RepoName })
                .HasName("PRIMARY")
                .HasAnnotation("MySql:IndexPrefixLength", new[] { 0, 0 });

            entity.ToTable("version_set");

            entity.HasIndex(e => e.PubId, "version_set_publish_log_FK");

            entity.HasIndex(e => e.RepoName, "version_set_repository_set_FK");

            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasColumnName("name");
            entity.Property(e => e.RepoName)
                .HasMaxLength(50)
                .HasColumnName("repo_name");
            entity.Property(e => e.Description)
                .HasMaxLength(1000)
                .HasColumnName("description");
            entity.Property(e => e.PubId)
                .HasMaxLength(50)
                .HasColumnName("pub_id");
            entity.Property(e => e.UpdateTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("update_time");
        });

        modelBuilder.Entity<WorkItem>(entity =>
        {
            entity.HasKey(e => new { e.Name, e.OwnerId, e.RepoName })
                .HasName("PRIMARY")
                .HasAnnotation("MySql:IndexPrefixLength", new[] { 0, 0, 0 });

            entity.ToTable("work_item");

            entity.HasIndex(e => e.RepoName, "work_item_repository_set_FK");

            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasColumnName("name");
            entity.Property(e => e.OwnerId)
                .HasMaxLength(64)
                .HasColumnName("owner_id");
            entity.Property(e => e.RepoName)
                .HasMaxLength(50)
                .HasColumnName("repo_name");
            entity.Property(e => e.Content)
                .HasColumnType("json")
                .HasColumnName("content");
            entity.Property(e => e.Description)
                .HasMaxLength(1000)
                .HasColumnName("description");
            entity.Property(e => e.OwnerName)
                .HasMaxLength(50)
                .HasColumnName("owner_name");
            entity.Property(e => e.Submitted).HasColumnName("submitted");
            entity.Property(e => e.UpdateTime)
                .HasDefaultValueSql("CURRENT_TIMESTAMP")
                .HasColumnType("timestamp")
                .HasColumnName("update_time");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
